package code.tools.bitmap
{
	import com.patience.map.isometric.iso.SingleIsoObject;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * 影片播放器
	 * @author Yuri
	 */
	public class MoviePlayer extends EventDispatcher
	{
		/**
		 * @param bitmap 用于显示bitmap 的对象
		 * @param bitmapX 原始bitmap的位置坐标
		 * @param bitmapY 原始bitmap的位置坐标
		 * @param bitmapArray bitmapData的数组
		 * @param frame 动画帧数，有效数值为5 10 15 30
		 */
		public function MoviePlayer(bitmap:*,dataList:Array = null,bitmapX:Number = 0,bitmapY:Number = 0,interval:int = 33)
		{
			super();
			dataArray = dataList;
			_bitmap = bitmap;
			_bitmapX = bitmapX;
			_bitmapY = bitmapY;
			_interval = interval;
		}
		
		private var _interval:int = 3;
		private var _isForever:Boolean;
		/**
		 * 播放完成事件
		 */
		public static const PLAY_COMP:String = "playComp";
		
		
		private var _bitmap:*;
		private var _dataArray:Array;
		private var _bitmapX:Number;
		private var _bitmapY:Number;
		
		private var _currentFrame:int = 0;
		
		private var _totalFrame:int = 0;
		
		private var _isStop:Boolean = false;
		
		/**
		 *是否添加了帧监听 
		 */
		private var _isAddFrameListener:Boolean = false;
		/*private var _isJumpStop:Boolean = false;
		private var _isJumpPlay:Boolean = false;*/
		public function get interval():int
		{
			return _interval;
		}
		
		public function set interval(value:int):void
		{
			_interval = value;
		}
		
		private function frameHandler():void
		{
			if(!_dataArray || _dataArray.length==0) return;
			if(_isStop) return;
			changeHandler();
		}
		
		/**
		 *发生改变 
		 * 
		 */
		private function changeHandler():void{
			_bitmap.x = _bitmapX + _dataArray[_currentFrame][0];
			_bitmap.y = _bitmapY + _dataArray[_currentFrame][1];
			_bitmap.bitmapData = _dataArray[_currentFrame][2];
			
			if(_currentFrame + 1 == _totalFrame)
			{
				if(_totalFrame>1)
				dispatchEvent(new Event(MoviePlayer.PLAY_COMP));
				_currentFrame = 0;
			}else{
				_currentFrame++;
			}
		}
		
		private function frameHandler2():void
		{
			if(!_dataArray || _dataArray.length==0) return;
			if(_isStop) return;
			_bitmap.setOffsetXY(_bitmapX + _dataArray[_currentFrame][0],_bitmapY + _dataArray[_currentFrame][1]);
			_bitmap.bitmapData = _dataArray[_currentFrame][2];
			
			if(_currentFrame + 1 == _totalFrame)
			{
				dispatchEvent(new Event(MoviePlayer.PLAY_COMP));
				_currentFrame = 0;
			}else{
				_currentFrame++;
			}
		}
		
		/**
		 * 开始播放
		 */
		public function start():void
		{
			_currentFrame = 0;
			_isStop = false;
			/*_isJumpPlay = false;
			_isJumpStop = false;*/
			
			if(_totalFrame==0) return;
			if(_totalFrame==1){
				goToAndStop(1);
				return;
			}
			
			if(this._bitmap as SingleIsoObject)
			{
				FrameDrive.addEventListener(frameHandler2, interval,_isForever);
			}else{
				FrameDrive.addEventListener(frameHandler,interval,_isForever);
			}
			_isAddFrameListener = true;
		}
		
		/**
		 * 停止
		 */
		public function stop():void
		{
			_isStop = true;
			/*_isJumpPlay = false;
			_isJumpStop = false;*/
			if(this._bitmap as SingleIsoObject)
			{
				FrameDrive.removeListener(frameHandler2);
			}else{
				FrameDrive.removeListener(frameHandler);
			}
			_isAddFrameListener = false;
		}
		
		/**
		 * 暂停
		 */
		public function pause():void
		{
			_isStop = true;
		}
		
		/**
		 * 继续播放
		 */
		public function play():void
		{
			_isStop = false;
			/*_isJumpPlay = false;
			_isJumpStop = false;*/
		}
		
		/**
		 *停到哪一帧 
		 * 
		 */
		public function goToAndStop(value:int):void{
			if(value>_totalFrame || value<1) return;
			_isStop = true;
			_currentFrame = value-1;
			changeHandler();
		}
		
		/**
		 *从哪一帧开始播放
		 * 
		 */
		public function goToAndPlay(value:int):void{
			if(value>_totalFrame || value<1) return;
			_currentFrame = value-1;
			changeHandler();
			_isStop = false;
			if(_totalFrame>1 && !_isAddFrameListener){
				if(this._bitmap as SingleIsoObject)
				{
					FrameDrive.addEventListener(frameHandler2, interval);
				}else{
					FrameDrive.addEventListener(frameHandler,interval);
				}
				_isAddFrameListener = true;
			}
		}
		
		public function get dataArray():Array
		{
			return _dataArray;
		}
		
		public function set dataArray(value:Array):void
		{
			_dataArray = value;
			if(_dataArray){
				_totalFrame = _dataArray.length;
			}
		}
		
		public function set bitmapX(value:Number):void
		{
			_bitmapX = value;
		}
		
		public function set bitmapY(value:Number):void
		{
			_bitmapY = value;
		}
		
		public function get bitmapX():Number
		{
			return _bitmapX;
		}
		
		public function get bitmapY():Number
		{
			return _bitmapY;
		}

		public function get isForever():Boolean
		{
			return _isForever;
		}

		public function set isForever(value:Boolean):void
		{
			_isForever = value;
		}

		
	}
}